const CryptoJS = require('crypto-js')

function random_str() {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210abcdefghijklmnopqrstuvwxyz0123456789';
    var maxPos = chars.length;
    var str = '';
    for (var i = 0; i < 32; i++) {
        str += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return str;
}
/**
 * 加密
 */
const interface_key = '16ed9ecc7d9011eab9c63c6aa7c68b67'

export function ecb_encode(word) {
    var word = String(new Date().getTime()) + '+admin4b67e4c11eab49a3c6aa7c68b67+' + random_str()
    // console.log('查看明文：', word)
    var key = CryptoJS.enc.Utf8.parse(interface_key);
    // console.log(srcs)
    var encrypted = CryptoJS.AES.encrypt(word, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}
/**
 * 解密
 */
export function ecb_decode(word) {
    var key = CryptoJS.enc.Utf8.parse(interface_key);
    var decrypt = CryptoJS.AES.decrypt(word, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

// test
// en_code = aes_encode('123456789012345+' + String(Date.now()))
// en_code = aes_encode(String(new Date().getTime()) + '+1234567890123456')
// console.log('加密：',en_code)
// console.log('解密：',aes_decode(en_code))